use crypto;
use crypto::buffer::{ReadBuffer, WriteBuffer};
fn get_cipher(key: &[u8]) {
    let iv: [u8; 16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    let encryptor = crypto::aes::cbc_encryptor(
        crypto::aes::KeySize::KeySize128,
        key,
        &iv,
        crypto::blockmodes::NoPadding,
    );
}

pub fn erypto(text: String, key: &[u8]) -> [u8; 128] {
    let iv: [u8; 16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    let mut encryptor = crypto::aes::cbc_encryptor(
        crypto::aes::KeySize::KeySize128,
        key,
        &iv,
        crypto::blockmodes::NoPadding,
    );
    let mut msg = crypto::buffer::RefReadBuffer::new(text.as_bytes());
    let mut buff_out: [u8; 128] = [0;128];
    let mut msg_out = crypto::buffer::RefWriteBuffer::new(&mut buff_out);
    encryptor.encrypt(&mut msg, &mut msg_out, false).expect("加密错误！");
    buff_out
}

pub fn derypto(en_text: [u8; 128], key: &[u8]) -> String {
    let iv: [u8; 16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    let mut dencryptor = crypto::aes::cbc_decryptor(
        crypto::aes::KeySize::KeySize128,
        key,
        &iv,
        crypto::blockmodes::NoPadding,
    );
    let mut buff_in: [u8; 128] = [0;128];
    let mut buff_out: [u8; 4096] = [0;4096];
    let mut input = crypto::buffer::RefReadBuffer::new(&mut buff_in);
    let mut output = crypto::buffer::RefWriteBuffer::new(&mut buff_out);
    dencryptor.decrypt(&mut input, &mut output, false);
    let mut final_result = Vec::<u8>::new();

    final_result.extend(
        output
            .take_read_buffer()
            .take_remaining()
            .iter()
            .map(|&i| i),
    );
    String::from_utf8(final_result).unwrap()
}
